package com.ophiux.websocket.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @desc: websocket配置类
 * @author: hhl
 * @date：2023/6/20 11:37
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    /**
     * 注册端点，发布或者订阅消息的时候，需要连接此端点
     *
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry
                //添加端点
                .addEndpoint("/endpoint-websocket")
                //设置允许访问的源（所有）
                .setAllowedOriginPatterns("*")
                //开始sockjs支持
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //设置消息代理(服务端推送给客户端的路径前缀)
        registry.enableSimpleBroker("/topic", "/chat");
        //设置消息发布订阅的头(客户端发送数据给服务器的一个前缀)
        registry.setApplicationDestinationPrefixes("/app");
        //指定用户发送（一对一）的主题前缀是“/user”
//        registry.setUserDestinationPrefix("/user");
    }
}